package com.by.core.group;

import com.by.core.ex.ValidateException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Hzy
 * @version 1.0
 * @date 2025/4/10  11:35
 * @description
 * @since jdk11
 */
public class V {
    public static void validate(BindingResult result) {
        //获取验证结果
        List<FieldError> errors = result.getFieldErrors();
        if(null != errors && errors.size() > 0){
            Map<String, String> errorMap = new HashMap<>();
            //遍历错误信息
            errors.forEach(el -> {
                errorMap.put(el.getField(),el.getDefaultMessage());
            });
            //有错误信息  抛出错误信息
            throw new ValidateException(errorMap);
        }
    }
}
